package com.impzx.iwframe.persist.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import com.impzx.iwframe.persist.dao.Dao;


public class DaoBase<T> implements Dao<T> {

	@Resource
	protected DefaultSqlSessionFactory sqlSessionFactory;

	protected String prefix;
	private int defaultPageSize = 10;
	
	@Override
	public List<T> select(Map<String, Object> params) {
		SqlSession session = this.sqlSessionFactory.openSession(true);
		@SuppressWarnings("unchecked")
		List<T> resulList = session.selectList(new StringBuilder().append(prefix).append(".select").toString(), params);
		session.close();
		return resulList;
	}

	@Override
	public List<T> selectByPage(Map<String, Object> params, int pageId) {
		params.put("pageId", pageId);
		params.put("pageSize", defaultPageSize);
		SqlSession session = this.sqlSessionFactory.openSession(true);
		@SuppressWarnings("unchecked")
		List<T> resulList = session.selectList(new StringBuilder().append(prefix).append(".select").toString(), params);
		session.close();
		return resulList;
	}

	@Override
	public T selectById(int id) {
		SqlSession session = this.sqlSessionFactory.openSession(true);
		@SuppressWarnings("unchecked")
		T t = (T) session.selectOne(new StringBuilder().append(prefix).append(".selectById").toString(), id);
		session.close();
		return t;
	}

	@Override
	public int insert(T t) {
		SqlSession session = this.sqlSessionFactory.openSession(true);
		int result = session.insert(new StringBuilder().append(prefix).append(".insert").toString(), t);
		return result;
	}

	@Override
	public T selectObject(T t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(T t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteById(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public T[] selectObjectList(T t) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
